/*
 * Copyright 2008 Les Hazlewood
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.pojodm.gis;

import org.pojodm.entity.Entity;
import org.pojodm.entity.Referenceable;

/**
 * TODO class JavaDoc
 *
 * @author Les Hazlewood
 */
public abstract class AbstractLocation extends Entity implements Referenceable {

    private String name = null;
    private String code = null;
    private Location parent = null;

    public AbstractLocation(){}

    public AbstractLocation( String nameOrCode ) {
        this( nameOrCode, nameOrCode );
    }

    public AbstractLocation( String name, String code ) {
        setName(name);
        setCode(code);
    }

    public AbstractLocation( String name, String code, Location parent ) {
        setName( name );
        setCode( code );
        setParent( parent );
    }

    public String getName() {
        return name;
    }

    public void setName( String name ) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode( String code) {
        this.code = code;
    }

    public Location getParent() {
        return parent;
    }

    public void setParent( Location parent ) {
        this.parent = parent;
    }

    public String getLabel() {
        return getName();
    }

    //subclass implementation of super's template method onEquals.
    public boolean onEquals( Entity o ) {
        if ( o instanceof AbstractLocation ) {
            AbstractLocation t = (AbstractLocation)o;
            return getName().equals( t.getName() ) &&
                   (getParent() != null ? getParent().equals( t.getParent() ) : t.getParent() == null );
        }

        return false;
    }

    public int hashCode() {
        int result = getName().hashCode();
        result = 31 * result + (getParent() != null ? getParent().hashCode() : 0);
        return result;
    }

    @Override
    @SuppressWarnings({"CloneDoesntDeclareCloneNotSupportedException"})
    public Object clone() {
        AbstractLocation clone = (AbstractLocation)super.clone();
        clone.setName( getName() );
        clone.setCode( getCode() );
        clone.setParent( getParent() );
        return clone;
    }

    @Override
    public StringBuffer toStringBuffer() {
        return new StringBuffer(getName());
    }
}
